/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.integrator.web;

import br.com.integrator.Revista;
import br.com.integrator.dao.RevistaDAORevImp;
import br.com.integrator.dao.DAORev;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ed
 */
public class ServletRevistas extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private Date strToDate(String data) throws Exception {
        if (data == null) {
            return null;
        }

        Date dataF = null;

        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy");
            long timestamp = dateFormat.parse(data).getTime();
            dataF = new Date(timestamp);
        } catch (ParseException pe) {
            throw pe;
        }
        return dataF;
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String cmd = request.getParameter("cmd");

        if (cmd == null) 
            cmd = "principal";
        
        DAORev dao;
        Revista revista = new Revista();
        if (cmd!= null || !cmd.equalsIgnoreCase("principal")) {

            try {
                String id = request.getParameter("id");
                if (id!= null) {
                    revista.setId(Integer.parseInt(id));
                }


                revista.setNome(request.getParameter("nome"));
                revista.setEditora(request.getParameter("editora"));
                revista.setPreco(Double.parseDouble(request.getParameter("preco")));
                revista.setEdicao(strToDate(request.getParameter("edicao"))
            
            );
            } catch (Exception ex){
            ex.printStackTrace();
            }
            }

            try{
            dao = new RevistaDAORevImp();
            RequestDispatcher rd = null;
            if(cmd.equalsIgnoreCase("listar")){
                
                List revistasList = dao.todasRevistas();
                request.setAttribute("revistasList", revistasList);
                rd = request.getRequestDispatcher("/mostrarRevistasCads.jsp");

            } else if(cmd.equalsIgnoreCase("adc")){
            
            dao.salvar(revista);
            rd = request.getRequestDispatcher("ServletRevistas?cmd=listar");

            }else if(cmd.equalsIgnoreCase("exc")){
                
                dao.excluir(revista);
                rd = request.getRequestDispatcher("ServletRevistas?cmd=listar");
                
            }else if(cmd.equalsIgnoreCase("atu")){
            
            revista = dao.procurarRevista(revista.getId());
            HttpSession session = request.getSession(true);
            session.setAttribute("revista", revista);
            rd = request.getRequestDispatcher("/FormAtuRevista.jsp");
            
            } else if (cmd.equalsIgnoreCase("atualizar")){
            
            dao.atualizar(revista);
            rd = request.getRequestDispatcher("ServletRevistas?cmd=listar");
            
            } else if(cmd.equalsIgnoreCase("principal")){
            
            rd = request.getRequestDispatcher("/index.jsp");
    
            } 

            rd.forward(request, response);
        
            } catch (Exception e){

            e.printStackTrace();
            throw new ServletException(e);
}
}

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
        public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
